package com.gzj.environment.admin.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzj.environment.framework.annotation.PermissionRequired;
import com.gzj.environment.framework.annotation.ResponseResult;
import com.gzj.environment.enums.log.ParamPosition;
import com.gzj.environment.model.system.criteria.ResourceCriteria;
import com.gzj.environment.model.system.dto.ResourceSaveDto;
import com.gzj.environment.model.system.entity.Resource;
import com.gzj.environment.model.system.vo.ResourceVo;
import com.gzj.environment.module.log.annotation.AccessLog;
import com.gzj.environment.module.system.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@ResponseResult
@RequestMapping("/sys/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 分页条件查询
     * @param criteria 查询条件
     * @return
     */
    @GetMapping
    @PermissionRequired("resource:list")
    @AccessLog(value = "条件查询资源信息")
    public Page<Resource> list(ResourceCriteria criteria) {
        return resourceService.page(criteria);
    }

    /**
     * 新增资源信息
     * @param resourceSaveDto 资源信息
     */
    @PostMapping
    @PermissionRequired("resource:add")
    @AccessLog(value = "新增资源信息", position = ParamPosition.BODY)
    public void add(@Valid @RequestBody ResourceSaveDto resourceSaveDto) {
        resourceService.add(resourceSaveDto);
    }

    /**
     * 根据id查询资源信息
     * @return
     */
    @GetMapping("/{id}")
    @PermissionRequired("resource:list")
    @AccessLog(value = "查询id为[{1}]的角色信息")
    public Resource findOne(@PathVariable("id") Long id) {
        return resourceService.findOne(id);
    }

    /**
     * 更新资源信息
     * @param resourceSaveDto 资源信息
     */
    @PutMapping
    @PermissionRequired("resource:edit")
    @AccessLog(value = "更新资源信息", position = ParamPosition.BODY)
    public void edit(@Valid @RequestBody ResourceSaveDto resourceSaveDto) {
        resourceService.edit(resourceSaveDto);
    }

    /**
     * 批量删除资源信息
     * @param ids 资源id的集合
     */
    @DeleteMapping
    @PermissionRequired("resource:delete")
    @AccessLog(value = "批量删除资源信息")
    public void delete(@RequestParam("ids") List<Long> ids) {
        resourceService.delete(ids);
    }

    /**
     * 查询所有的权限
     * @return
     */
    @GetMapping("/all")
    @PermissionRequired("resource:list")
    @AccessLog(value = "查询所有的权限")
    public List<ResourceVo> findAll() {
        return resourceService.findAll();
    }

    /**
     * 根据角色id查询资源id的集合
     * @param roleId 角色id
     * @return
     */
    @GetMapping("/ids/{roleId}")
    @PermissionRequired("resource:list")
    @AccessLog(value = "根据角色id[{1}]查询资源id的集合")
    public List<String> findIdsByRoleId(@PathVariable("roleId") Long roleId) {
        return resourceService.findIdsByRoleId(roleId);
    }

    /**
     * 根据父id查询资源信息
     * @param parentId 父id
     * @return
     */
    @GetMapping("/parent/{parentId}")
    @PermissionRequired("resource:list")
    @AccessLog(value = "根据父id[{1}]查询")
    public List<Resource> findByParentId(@PathVariable("parentId") Long parentId) {
        return resourceService.findByParentId(parentId);
    }
}
